package com.sskj.fabi.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.fabi.bean.OrderDetailBean;
import com.sskj.fabi.http.HttpConfig;
import com.sskj.fabi.ui.activity.FabiOrderDetailActivity;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;

import io.reactivex.Flowable;


public class FabiOrderDetailActivityPresenter extends BasePresenter<FabiOrderDetailActivity> {

    public void getData(String id, boolean isLoading) {
        httpService.getOrderList(1, 20, false, id)
                .execute(new JsonCallBack<HttpData<PageBean<OrderDetailBean>>>(null) {
                    @Override
                    public void onSuccess(Response<HttpData<PageBean<OrderDetailBean>>> response) {
                        HttpData<PageBean<OrderDetailBean>> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.updateUI(httpData.getData().getList().get(0));
                        }
                    }
                });
    }

    public Flowable<OrderDetailBean> getData(String id) {
        return httpService.getOrderList(1, 20, false, id)
                .converter(new JsonConverter<HttpData<PageBean<OrderDetailBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(pageBeanHttpData -> pageBeanHttpData.getData().getList().get(0));

    }

    /**
     * 标记已付款
     *
     * @param orderNum
     */
    public void markPay(String orderNum) {
        httpService.markPay(orderNum)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        ToastUtil.showShort(response.body().getMsg());
                        if (response.body().getStatus() == HttpConfig.OK) {
                            mView.refresh();
                        }
                    }
                });
    }

    /**
     * 取消订单
     *
     * @param orderNum
     */
    public void cancelOrder(String orderNum) {

        httpService.cancelFabiTradeOrder(orderNum)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        ToastUtil.showShort(response.body().getMsg());

                        if (response.body().getStatus() == HttpConfig.OK) {
                            mView.refresh();
                        }
                    }
                });
    }

    /**
     * 账单申诉
     *
     * @param orderNum
     */
    public void allegeOrder(String orderNum, String ss_type, String ss_reason) {
        httpService.allegeOrder(orderNum, ss_type, ss_reason)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.getMsg());
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.refresh();
                        }
                    }
                });
    }

    /**
     * 确认放行
     *
     * @param orderNum
     * @param pwd
     */
    public void letGo(String orderNum, String pwd) {
        httpService.letGo(orderNum, pwd)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.getMsg());
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.refresh();
                        }
                    }
                });
    }
}
